package com.zmx.doctor.view.activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.netease.nim.avchatkit.okhttp.OkHttp3ClientManager;
import com.netease.nim.avchatkit.ui.MyDialog;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 程序猿：胖胖祥
 * 时 间：2020/10/17  0:16
 * 功 能：患者病例
 */
public class PatientsCasesActivity extends BaseActivity {

    @BindView(R.id.main_suit)
    EditText mainSuit;
    @BindView(R.id.now_medical_history)
    EditText nowMedicalHistory;
    @BindView(R.id.past_medical_history)
    EditText pastMedicalHistory;
    @BindView(R.id.genetic)
    EditText genetic;
    @BindView(R.id.allergy)
    EditText allergy;
    @BindView(R.id.the_diagnosis)
    EditText theDiagnosis;
    @BindView(R.id.regular)
    TextView regular;
    @BindView(R.id.notice)
    TextView notice;
    @BindView(R.id.submit)
    Button submit;
    private String ask_id, patient_id;
    private String medical_id = "";

    private Integer type;

    private String disease_name="";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_patients_cases;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);

        ask_id = this.getIntent().getStringExtra("ask_id");
        patient_id = this.getIntent().getStringExtra("patient_id");
        theDiagnosis.setFocusable(false);
        isMedical();

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:
                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        type = object.getInt("code");

                        if (type == 1) {

                            JSONObject data = object.getJSONObject("data");
                            mainSuit.setText(data.getString("sketch"));
                            nowMedicalHistory.setText(data.getString("now"));
                            pastMedicalHistory.setText(data.getString("history"));
                            genetic.setText(data.getString("family"));
                            allergy.setText(data.getString("allergy"));
                            regular.setText(data.getString("marry"));
                            notice.setText(data.getString("birthday"));
                            medical_id = data.getString("id");
                            theDiagnosis.setText(data.getString("disease_name"));
                            disease_name = data.getString("disease_name");

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                    break;


                case 9:
                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Toast.makeText(mActivity,object.getString("msg"), Toast.LENGTH_SHORT).show();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;
            }

        }
    };


    //获取病例
    public void isMedical() {

        List<String> ls = new ArrayList<>();
        ls.add(ask_id);
        ls.add(patient_id);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "isMedical");
        params.put("data[ask_id]", ask_id);
        params.put("data[patient_id]", patient_id);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor/", params, handler, 1, 404);
    }


    @OnClick({R.id.regular, R.id.notice, R.id.the_diagnosis,R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.regular:
                showList(regular);
                break;

            case R.id.submit:

                if (type == 0) {

                    addMedical(mainSuit.getText().toString(), pastMedicalHistory.getText().toString(), allergy.getText().toString(), nowMedicalHistory.getText().toString(), genetic.getText().toString(), regular.getText().toString(), notice.getText().toString());

                } else {

                    editMedical(mainSuit.getText().toString(), pastMedicalHistory.getText().toString(), allergy.getText().toString(), nowMedicalHistory.getText().toString(), genetic.getText().toString(), medical_id, regular.getText().toString(), notice.getText().toString());

                }

                break;

            case R.id.notice:
                showNotice(notice);
                break;
            case R.id.the_diagnosis:

                Intent intent = new Intent(mActivity, DiseaseNameActivity.class);
                startActivityForResult(intent, 1);//带请求码打开activity (请求码自定 这里设为1

                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {  //根据请求码可处理不同活动返回的数据

            case 1:

                if (data != null){

                    //操作
                    disease_name = data.getExtras().getString("name");
                    theDiagnosis.setText(disease_name);

                }
                break;
        }
    }

    private AlertDialog alertDialog1; //信息框

    public void showList(TextView textView) {

        final String[] items = {"已婚", "未婚"};
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(mActivity);
        alertBuilder.setTitle("请选择");
        alertBuilder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                textView.setText(items[i]);
                alertDialog1.dismiss();
            }
        });
        alertDialog1 = alertBuilder.create();
        alertDialog1.show();
    }

    public void showNotice(TextView textView) {
        final String[] items = {"未生", "一胎", "二胎"};
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(mActivity);
        alertBuilder.setTitle("请选择");
        alertBuilder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                textView.setText(items[i]);
                alertDialog1.dismiss();
            }
        });
        alertDialog1 = alertBuilder.create();
        alertDialog1.show();
    }



    public void editMedical(String sketch,String history,String allergy,String now,String family,String medical_id,String regular,String notice) {

        List<String> ls = new ArrayList<>();
        ls.add(allergy);
        ls.add(ask_id);
        ls.add(notice);
        ls.add(disease_name);
        ls.add(family);
        ls.add(history);
        ls.add(regular);
        ls.add(medical_id);
        ls.add(now);
        ls.add(patient_id);
        ls.add(sketch);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "editMedical");
        params.put("data[ask_id]", ask_id);
        params.put("data[patient_id]", patient_id);
        params.put("data[sketch]", sketch);
        params.put("data[history]", history);
        params.put("data[allergy]", allergy);
        params.put("data[now]", now);
        params.put("data[family]", family);
        params.put("data[marry]", regular);
        params.put("data[medical_id]", medical_id);
        params.put("data[birthday]", notice);
        params.put("data[disease_name]", disease_name);
        com.netease.nim.avchatkit.okhttp.OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor/", params, handler, 9, 404);
    }



    public void addMedical(String sketch,String history,String allergy,String now,String family,String regular,String notice) {

        List<String> ls = new ArrayList<>();
        ls.add(allergy);
        ls.add(ask_id);
        ls.add(notice);
        ls.add(disease_name);
        ls.add(family);
        ls.add(history);
        ls.add(regular);
        ls.add(now);
        ls.add(patient_id);
        ls.add(sketch);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "addMedical");
        params.put("data[ask_id]", ask_id);
        params.put("data[patient_id]", patient_id);
        params.put("data[sketch]", sketch);
        params.put("data[history]", history);
        params.put("data[allergy]", allergy);
        params.put("data[now]", now);
        params.put("data[family]", family);
        params.put("data[marry]", regular);
        params.put("data[birthday]", notice);
        params.put("data[disease_name]", disease_name);
        com.netease.nim.avchatkit.okhttp.OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/doctor/", params, handler, 9, 404);
    }


}
